package knapsackProblem;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Knapsack {
	public Knapsack(int capacity) {
		this.capacity = capacity;
	}

	private int capacity;

	private Item calcCurrentState(List<KnapsackItem> previousStates,
			Set<KnapsackItem> notAdmissibleStates) {
		int profit = 0;
		long time = 0;
		for (KnapsackItem item : previousStates) {
			profit += item.getProfit();
			time += item.getTime();
			if (notAdmissibleStates.size() > 10) {
				for (KnapsackItem knapsackItem : notAdmissibleStates) {
					System.out.println(knapsackItem.getId());
				}
			}

			if (!notAdmissibleStates.contains(item))
				notAdmissibleStates.add(item);
		}
		return new Item(profit, time);
	}

	private List<KnapsackItem> getGoalStates(Item currentState,
			Set<KnapsackItem> generalizedStates,
			Set<KnapsackItem> notAdmissibleStates) {
		List<KnapsackItem> goalStates = new ArrayList<KnapsackItem>();
		for (KnapsackItem knapsackItem : generalizedStates) {
			if (!notAdmissibleStates.contains(knapsackItem)) {
				if (currentState.time + knapsackItem.time > this.capacity)
					notAdmissibleStates.add(knapsackItem);
				else
					goalStates.add(knapsackItem);
			}
		}
		return goalStates;
	}

	public List<KnapsackItem> getGoalStates(List<KnapsackItem> previousStates,
			Set<KnapsackItem> generalizedStates,   
			Set<KnapsackItem> notAdmissibleStates) {
		Item currentState = this.calcCurrentState(previousStates,
				notAdmissibleStates);
		List<KnapsackItem>	goalStates = this.getGoalStates(currentState, generalizedStates,
				notAdmissibleStates);
		return goalStates;
	}
}